<?php
class Sosweb_View_Helper_CouponGrid
{
	public function couponGrid($coupons, $baseUrl, $sortLink, $categories, $category, $messageText)
	{
		$categoryLink = '';
		
		if ($category != null && strlen($category) > 0)
		{
			$categoryLink = '/category/'.$category;
		}
		
		//header
		$html = 
  			'<table class="fullW fullH"> '.
			' <tr> '.
		  	'  <td class="couponsCount" colspan="4"> '.
			$messageText.count($coupons).
			'  </td> '.
			' </tr> '.
			' <tr>'.
			'  <td class="couponsData header"> ';
			
		if ($sortLink != null)
		{
			$html .=
				'   <a class="couponsHeader" href="'.$baseUrl.$sortLink.'/sort/title'.$categoryLink.'" title="Sortuj po tytule kuponu."> '.
				'Dane kuponu'.
				'   </a> ';
		}	
		else
		{
			$html .=
				'Dane kuponu';
		}
		
		$html .=	
			'  </td> '.
			'  <td class="couponsLogo header"> '.
			'   Logo '.
			'  </td> ';
			
		$html .=
			'  <td class="couponsPostDate header"> ';
		
		if ($sortLink != null)
		{
			$html .=
				'   <a class="couponsHeader" href="'.$baseUrl.$sortLink.'/sort/posted_date'.$categoryLink.'" title="Sortuj po dacie dodania."> '.
				'Data rozpoczęcia'.
				'   </a> ';
		}	
		else
		{
			$html .=
				'Data rozpoczęcia';
		}		
		
		$html .=
			'  </td> '.
			'  <td class="couponsExpiryDate header"> ';

			if ($sortLink != null)
		{
			$html .=
				'   <a class="couponsHeader" href="'.$baseUrl.$sortLink.'/sort/expiry_date'.$categoryLink.'" title="Sortuj po dacie wygaśnięcia."> '.
				'Data wygaśnięcia'.
				'   </a> ';
		}	
		else
		{
			$html .=
				'Data wygaśnięcia';
		}		

		$html .=
			'  </td> '.
			' </tr>';
		
		if (count($coupons) > 0)
		{	
			$loopCounter = 0;			
			foreach ($coupons as $key=>$value)
			{
				$html .=
					' <tr> '.
					'  <td class="couponsData'.($loopCounter%2 == 0 ? '' : ' dark').'"> '.
					'   <table class="fullW fullH"> '.
					'	 <tr> '.
					'	  <td class="couponsDataTitle"> ';
				
				if ($value->t_link != null)
				{
					$html .= 
						'<a class="couponsTitle" href="'.$baseUrl.'/Goto/title/id/'.$value->id.'" title="Przejdź na stronę sklepu." target="_blank"> '.
						$value->title.
						'</a> ';
				}
				else
				{
					$html .= 
						'<b>'.$value->title.'</b>';
				
				}
				
				if ($value->icon != null)
				{
					$html .=
						'&nbsp;'.
						'	   <img src="'.$baseUrl.'/public/images/'.$value->icon.'" alt="Kategoria '.$value->category.'" /> '.
						'	  </td> ';
				}
				else
				{
					$html .= 
						'	  </td> ';					
				}
								
				$html .=
					'	 </tr> '.
					'	 <tr> '.
					'	  <td class="couponsDataContent"> '.
					$value->content.
					'	  </td> '.				
					'	 </tr> '.				
					'	 <tr> '.
					'	  <td class="couponsDataCode"> ';
					
				if ($value->c_link != null)
				{
					$html .= 
						
						'	   KOD: <a class="couponsCode" href="'.$baseUrl.'/Goto/code/id/'.$value->id.'" title="Przejdź na stronę produktu." target="_blank">'.$value->code.'</a> ';
				}
				else
				{
					$html .= 
						'	   KOD: <b>'.$value->code.'</b>';
				}					

				$html .=
					'	  </td> '.					
					'	 </tr> '.				
					'	</table> '.
					'  </td> '.
					'  <td class="couponsLogo normal'.($loopCounter%2 == 0 ? '' : ' dark').'"> ';
				
				if ($value->logo != null) 
				{
					$imgSize = Sosweb_Util_Image_Processing::scale('files/upload/'.$value->logo, 100, 100);
					$imgWidth = $imgSize['width'];
					$imgHeight = $imgSize['height'];
					
					$html .=
						'<img src="'.$baseUrl.'/public/files/upload/'.$value->logo.'" alt="logo" width="'.$imgWidth.'" height="'.$imgHeight.'" />';
				}

				$html .=	
					'  </td> '.				
					'  <td class="couponsPostDate normal'.($loopCounter%2 == 0 ? '' : ' dark').'"> '.
					$value->posted_date.
					'  </td> '.
					'  <td class="couponsExpiryDate normal'.($loopCounter%2 == 0 ? '' : ' dark').'"> '.
					$value->expiry_date.
					'  </td> '.								
					' </tr> ';
				
				$loopCounter++;
			}
	
			$html .=
				'</table> ';			
		}
		else
		{
			$html .=
				 '<table> '.
				 ' <tr> '.
				 '  <td class=""> '.
				 'Brak kuponów do wyświetlenia.'.
				 '  </td> '.
				 ' </tr> '.
				 '</table>';
		}
		
		return $html;
	}
}